import React from 'react'
import {connect} from 'react-redux'
import BasicInfo from 'components/basic-info';
import {messages} from 'share/common';
import { Button, Input, message, Affix } from 'antd';
import ResizeTable from 'components/resize-table'
import SlideFrame from 'components/slide-frame';
import mateRule from 'containers/financial-account/interface-definition/mate-rule';
import menuRoute from "routes/menuRoute";
import 'styles/setting/wait-for-billing/invoice-status-query.scss';
import { setSettingMate } from 'actions/financial-account';
import interfaceService from "containers/financial-account/interface-definition/interface-service";
const Search = Input.Search;

class settingMata extends React.Component{
  constructor(props){
    super(props);
    this.state = {
      infoList: [
        {type: 'input', id: 'sceneCode', label: messages('financial-account.key43')/*场景编码*/},
        {type: 'input', id: 'sceneName', label: messages('financial-account.key44')/*场景名称*/},
        {type: 'input', id: 'comment', label: messages('financial-account.key45')/*场景说明*/},
        {type: 'switch', id: 'isDefault', label: messages('financial-account.key5')/*是否预置*/},
        {type: 'switch', id: 'isEnable', label: messages('common.column.status')/*状态*/},
      ],
      infoData: {},
      columns: [
        {title: messages('financial-account.key106')/*优先级*/,dataIndex: 'priority', width: 80},
        {title: messages('financial-account.key104')/*匹配组编码*/,dataIndex: 'groupCode'},
        {title: messages('financial-account.key105')/*匹配组名称*/,dataIndex: 'groupName'},
        {title: messages('financial-account.key5')/*是否预置*/, width: 96,dataIndex: 'isDefault', render:(value) => value ? messages('common.yes')/*是*/ : messages('common.no')/*否*/},
        {title: messages('common.column.status')/*状态*/,dataIndex: 'isEnable', width: 80, render:(value) => value ? messages('common.valid')/*生效*/ : messages('financial-account.key6')/*不生效*/},
        {title: messages('financial-account.key121')/*配置匹配值*/, width: 150, dataIndex: 'operation', render: (value,record) => {
            return <span>
              <a onClick={e => this.handleEdit(e,record)}>{messages('common.edit')/*编辑*/}</a>
              <span className="ant-divider"/>
              <a onClick={e => this.handleGoDetail(e,record)}>{messages('financial-account.key122')/*科目映射*/}</a>
            </span>;}}
      ],
      data: [],
      loading: false,
      pagination: {
        total: 0
      },
      page: 0,
      title: '',
      editRecord: {},
      showSlideFrame: false,
      objectMapping: menuRoute.getRouteItem('object-mapping'),
      searchKey: ''
    }
  }

  componentDidMount() {
    this.props.dispatch(setSettingMate({}));
    this.setState({infoData: this.props.sceneDefinition},() => {
      this.getList();
    });
  }

  getList = () => {
    const { searchKey, page, infoData } = this.state;
    let temp = {
      sceneDefineId: infoData.id,
      groupKey: searchKey
    };
    Object.keys(temp).map(item => {
      !temp[item] && delete temp[item];
    });
    temp.page = page;
    temp.size = 10;
    this.setState({loading: true});
    interfaceService.getMateList(temp).then(res => {
      this.setState({
        loading: false,
        data: res.data.rows,
        pagination: {
          total: Number(res.data.total),
          current: page + 1,
          onChange: this.onChangePager,
        }
      });
    }).catch(e => {
      message.error(e.response.data.message);
      this.setState({loading: false});
    })
  };

  onChangePager = (page) => {
    if (page - 1 !== this.state.page)
      this.setState({
        page: page - 1
      }, () => {
        this.getList();
      })
  };

  handleChangeKey = (e) => {
    this.setState({searchKey: e.target.value, page: 0},() => {
      this.getList();
    })
  };

  handleNew = () => {
    this.setState({
      title: messages('financial-account.key123')/*新建匹配组*/,
      editRecord: {}
    },() => {
      this.setState({showSlideFrame: true});
    })
  };

  handleEdit = (e,record) => {
    e.stopPropagation();
    this.setState({
      title: messages('financial-account.key124')/*编辑匹配组*/,
      editRecord: record
    },() => {
      this.setState({showSlideFrame: true});
    })
  };

  handleGoDetail = (e,record) => {
    e.stopPropagation();
    this.props.dispatch(setSettingMate(record));
    this.context.router.push(this.state.objectMapping.url.replace(':tab','accounting-background-definition'));
  };

  isRefresh = (isRefresh) => {
    this.setState({showSlideFrame: false});
    isRefresh === true && this.getList();
  };

  goBack = () => {
    this.context.router.push(menuRoute.getRouteItem('interface-definition-detail').url.replace(':tab','accounting-background-definition'));
  };

  render(){
    const { infoList, infoData, columns, data, loading, pagination, title, showSlideFrame, editRecord } = this.state;
    return(
      <div className="invoice-status">
        <BasicInfo infoList={infoList}
                   infoData={infoData}
                   isHideEditBtn={true}/>
        <div style={{margin:'10px 0px'}}>
          <Button type="primary" onClick={this.handleNew}>{messages('common.create')}</Button>
          <Search onChange={this.handleChangeKey}
                  style={{display:'block',float:'right',width:'400px',top:'50%'}}
                  placeholder={messages('financial-account.key72')/*请输入编码/名称*/}/>
        </div>
        <ResizeTable
          columns={columns}
          dataSource={data}
          loading={loading}
          rowKey={'id'}
          pagination={pagination}
          bordered/>
        <SlideFrame show={showSlideFrame}
                    title={title}
                    params={{
                      showSlideFrame,
                      editRecord
                    }}
                    content={mateRule}
                    onClose={(isRefresh) => this.isRefresh(isRefresh)}
                    afterClose={(isRefresh) => this.isRefresh(isRefresh)}/>
        <Affix offsetBottom={0} className="bottom-bar">
          <Button onClick={this.goBack}>{messages('common.back')/*返回*/}</Button>
        </Affix>
      </div>
    )
  }
}

function mapStateToProps(state){
  return {
    sceneDefinition: state.financialAccount.sceneDefinition
  }
}

settingMata.contextTypes = {
  router: React.PropTypes.object
};

export default connect(mapStateToProps)(settingMata);
